import gov.nih.mipav.model.structures.ModelImage;
import gov.nih.mipav.view.ViewUserInterface;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
import java.util.Enumeration;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class MaskManager extends Module implements Drawable,
    MethodRelated {

  private DefaultComboBoxModel cbMaskModel;

  public MaskManager() {
    super();
    cbMaskModel = new DefaultComboBoxModel();
  }

  public Component drawContentPane() {
    JPanel maskPane = new JPanel();
    maskPane.setBorder(new TitledBorder("Mask Selector"));
    maskPane.setLayout(new BorderLayout(2, 2));
    JComboBox maskComboBox = new JComboBox(cbMaskModel);
    maskComboBox.setMinimumSize(new Dimension(100, 20));
    maskPane.add(maskComboBox, BorderLayout.CENTER);

    return maskPane;
  }

  public void setEnable(boolean enable) {

  }

  protected void updateMe() {

    ViewUserInterface userInterface = ViewUserInterface.getReference();

    String selectedMask = (String) cbMaskModel.getSelectedItem();
    cbMaskModel.removeAllElements();
    cbMaskModel.addElement("<NONE>");

    int numberOfImages = userInterface.getRegisteredImagesNum();
    if (numberOfImages > 0) {
      Enumeration<String> names = userInterface.getRegisteredImageNames();
      while (names.hasMoreElements()) {
        String temp = names.nextElement();
        cbMaskModel.addElement(temp);
      }

    }
    if (cbMaskModel.getIndexOf(selectedMask) >= 0)
      cbMaskModel.setSelectedItem(selectedMask);

  }

  public void updateProxy() {
    JSMethodProxy jsProxy = JSMethodProxy.getInstance();
    ViewUserInterface userInterface = ViewUserInterface.getReference();

    ModelImage maskModel = null;
    short[] mask = null;

    try {
      if (!(cbMaskModel.getSelectedItem() == cbMaskModel.getElementAt(0))) {
        maskModel = userInterface.getRegisteredImageByName((String) cbMaskModel
            .getSelectedItem());

        int pixels = maskModel.getSize();

        mask = new short[pixels];
        maskModel.exportData(0, pixels, mask);

      } else {
        mask = new short[1];
        mask[0] = -1; // CODE TO FIND OUT IF THERE IS NOT MASK SETTED
      }
      jsProxy.setMask(mask);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public String getMaskName() {
    return (String) cbMaskModel.getSelectedItem();
  }

  /**
   * Only needs to be update in case the loaded images changed
   */
  @Override
  public boolean needsToBeUpdated() {
    boolean update = false;

    ViewUserInterface userInterface = ViewUserInterface.getReference();
    Enumeration<String> currentNames = userInterface.getRegisteredImageNames();

    int currentNamesLength = 1;
    while (currentNames.hasMoreElements()) {
      currentNamesLength++;
      String temp = currentNames.nextElement();
      update = update || (cbMaskModel.getIndexOf(temp) == -1);
    }

    update = update || (cbMaskModel.getSize() != currentNamesLength);
    return update;
  }

}
